package com.gsy.controller.student;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gsy.aop.annotation.TokenValid;
import com.gsy.entity.ReadEntity;
import com.gsy.enumerate.SystemStatusEnum;
import com.gsy.response.BaseResponse;
import com.gsy.service.NoticeService;
import com.gsy.service.ReadService;
import com.gsy.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gsy
 * @since 2022-03-03
 */
@RestController
@RequestMapping(value = "students", consumes = "application/json", produces = "application/json")
@Api(value = "controller", tags = {"tb_read 操作接口"})
public class ReadController {

    private final static Logger LOGGER = LoggerFactory.getLogger(ReadController.class);

    @Resource
    private ReadService readService;

    /**
    * 获取的我通知已读列表(供已读未读状态渲染
    * @param uId
    * @return
    */
    @ApiOperation(value = "获取的我通知已读列表(供已读未读状态渲染", notes = "获取的我通知已读列表(供已读未读状态渲染")
    @GetMapping("getNoticeRead/{uId}")
    @TokenValid
    public BaseResponse getReadList(@PathVariable Integer uId) {

        List<ReadEntity> entities = readService.list(Wrappers.lambdaQuery(new ReadEntity())
                .eq(ReadEntity::getUId, uId));
        if (CollectionUtil.isNotEmpty(entities)) {
            BaseResponse success = new BaseResponse(SystemStatusEnum.SUCCESS);
            success.setData(entities);
            return success;
        } else {
            return new BaseResponse(SystemStatusEnum.SUCCESS.getCode(), "获取数据为空！");
        }
     }
    /**
    * 已读转未读
    * @param request
    * @return
    */
    @ApiOperation(value = "已读转未读", notes = "已读转未读")
    @GetMapping("goweidu/{nId}/{uId}")
    @TokenValid
    public BaseResponse goweidu(@PathVariable Integer nId,@PathVariable Integer uId) {

        ReadEntity entity = readService.getOne(Wrappers.lambdaQuery(new ReadEntity()).eq(ReadEntity::getNId, nId)
                .eq(ReadEntity::getUId, uId));
        if (entity == null) {
            return new BaseResponse(SystemStatusEnum.FAIL.getCode(), "记录不存在！");
        }

        readService.removeById(entity.getRId());
        return new BaseResponse(SystemStatusEnum.SUCCESS);

     }
    /**
    * 未读转已读
    * @return
    */
    @ApiOperation(value = "未读转已读", notes = "未读转已读")
    @GetMapping("goyidu/{nId}/{uId}")
    @TokenValid
    public BaseResponse goyidu(@PathVariable Integer nId,@PathVariable Integer uId) {

        List<ReadEntity> entity = readService.list(Wrappers.lambdaQuery(new ReadEntity())
                .eq(ReadEntity::getUId, uId)
                .eq(ReadEntity::getNId, nId));
        if (CollectionUtil.isNotEmpty(entity)) {
            return new BaseResponse(SystemStatusEnum.FAIL.getCode(),"状态已经是已读状态");
        }
        ReadEntity readEntity = new ReadEntity();
        readEntity.setUId(uId);
        readEntity.setNId(nId);
        readEntity.setReadtime(new Date());

        readService.save(readEntity);
        return new BaseResponse(SystemStatusEnum.SUCCESS);

     }

}
